﻿using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;

using _DBG = Microsoft.SPOT.Debugger;
using _WP = Microsoft.SPOT.Debugger.WireProtocol;

namespace HelpNetwork
{
    public class SerialDump
    {
        _DBG.Engine m_eng;
        bool m_fTimestamp;
        bool m_fUsb = true;
        public bool serialIsConnected = false;
        bool m_fNewLine = true;

        //--//

        public SerialDump()
        {
            _DBG.PortDefinition pd = null;
            string port = null;
            
            if (m_fUsb)
            {
                _DBG.PortDefinition[] ports = _DBG.AsyncUsbStream.EnumeratePorts();

                if (port == null)
                {
                    if (ports.Length == 0)
                    {
                        MessageBox.Show("Nu s-a detectat placa de captura IPR2400. Va rugam inserati placa intr-un port USB si reporniti aplicatia.", "Eroare fatala", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Principal.mainForm.inserareMesajStare("Nu s-a detectat placa de captura IPR2400. Va rugam inserati placa intr-un port USB si reporniti aplicatia.");
                        
                        Principal.mainForm.iesire = true;
                    }
                    else if (ports.Length == 1)
                    {
                        pd = ports[0];
                    }
                    else
                    {
                        Principal.mainForm.inserareMesajStare("S-au detecat mai multe dispozitive de tip USB SPOT");
                        Principal.mainForm.inserareMesajStare("Deconectati unul dintre ele.");
                        Principal.mainForm.iesire = true;
                    }
                }
            }

            m_eng = new _DBG.Engine(pd);
        }

        public void Run()
        {
            m_eng.Silent = true;

            m_eng.Start();

            m_eng.OnNoise += new _DBG.NoiseEventHandler(OnNoise);
            m_eng.OnMessage += new _DBG.MessageEventHandler(OnMessage);
        }
        public void Stop()
        {
            m_eng.Stop();
            m_eng = null;
        }

        void OnNoise(byte[] buf, int offset, int count)
        {
            HandleOutput(buf, offset, count);
        }

        void OnMessage(_WP.IncomingMessage msg, string text)
        {
            int indexRet = text.LastIndexOf("\r");
            text = text.Remove(indexRet);

            if (text.Length > 5)
            {
                HandleOutput(text);
            }
        }

        void HandleOutput(byte[] buf, int offset, int count)
        {
            while (count-- > 0)
            {
                if (m_fTimestamp)
                {
                    if (m_fNewLine)
                    {
                        HandleOutput(String.Format("[{0:HH:mm:ss:fff}]  ", DateTime.Now));
                    }
                }

                char c = (char)buf[offset++];

                HandleOutput(new String(c, 1));

                m_fNewLine = (c == '\n');
            }
        }

        void HandleOutput(string text)
        {
            Principal.serialRecv = text;
            Principal.mainForm.inserareMesajStare(text);
            if (serialIsConnected == false)
            {
                Principal.mainForm.serialConectat = true;
                serialIsConnected = true;
            }
        }
    }
}
